<?php

namespace app\modules\admin\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use app\modules\admin\components\ArrayHelper;
/**
 * This is the model class for table "sys_menu".
 *
 * @property int $id
 * @property int $pid 父id
 * @property int $cate_id 分类id
 * @property string $name 分类名称
 * @property string $url 链接url
 * @property string $icon 连接icon
 * @property int $sort 排序
 * @property int $level 菜单级别
 * @property int $status 菜单状态[-1:删除,0:禁用,1:启用]
 * @property int $created_at 添加时间
 * @property int $updated_at 更新时间
 */
class SysMenu extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'sys_menu';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['pid', 'cate_id', 'sort', 'level', 'status', 'created_at', 'updated_at'], 'integer'],
            [['name', 'cate_id'], 'required'],
            ['url', 'default', 'value' => '#'],
            [['name', 'icon'], 'string', 'max' => 32],
            [['url'], 'string', 'max' => 64],
            [['params'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'pid' => 'Pid',
            'cate_id' => 'Cate ID',
            'name' => '名称',
            'url' => '路由',
            'icon' => '图标',
            'sort' => '排序',
            'level' => '等级',
            'status' => '状态',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getCate()
    {
        return $this->hasOne(SysMenuCate::className(), ['id' => 'cate_id']);
    }

    public static function getList($status = false)
    {
        $data = self::find()->andFilterWhere(['status' => $status]);
        $models = $data->orderBy('cate_id asc, sort asc')
                    ->with(['cate'])
                    ->asArray()
                    ->all();
        return ArrayHelper::itemsMerge($models);
    }
}
